//
//  PhotoHeroLayer.swift
//  SwiftUILean
//
//  Created by wyknmjj on 2024/5/13.
//

import SwiftUI

struct PhotoHeroLayer: View {
    @Environment(PhotoCoordinator.self) private var coordinator
    var item:PhotoItem
    var sAnchor:Anchor<CGRect>
    var dAnchor:Anchor<CGRect>
    var body: some View {
        GeometryReader{ proxy in
            let sRect=proxy[sAnchor]
            let dRect=proxy[dAnchor]
            let animateView = coordinator.animateView
            
            let viewSize:CGSize = animateView ? dRect.size : sRect.size
            let viewPosition:CGSize = animateView 
            ? .init(width: dRect.minX, height: dRect.minY)
            : .init(width: sRect.minX, height: sRect.minY)
            
            
            if let image=item.image,!coordinator.showDetailView{
                Image(uiImage: image)
                    .resizable()
                    .aspectRatio(contentMode: animateView ? .fit : .fill)
                    .frame(width: viewSize.width,height: viewSize.height)
                    .clipped()
                    .offset(viewPosition)
                    .transition(.identity)
//                    .opacity(0.4)
            }
        }
    }
}

#Preview {
    ContentView()
}
